package com.glp.work.flow.controller;

import com.glp.common.reponse.BaseResponse;
import com.glp.common.reponse.BaseResult;
import com.glp.oss.client.SimpleOssClient;
import com.glp.oss.client.StsTokenVo;
import com.glp.work.flow.constant.APICode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author : 郭永伟
 * @date : 2020/09/15/11:23
 * @description :
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("aliyun")
@Api(tags = "阿里云配置")
public class AliyunController {
    @NonNull
    private final SimpleOssClient simpleOssClient;

    @NonNull
    private final RedisTemplate redisTemplate;

    @ApiOperation("阿里云配置")
    @GetMapping("/stsToken")
    public BaseResult stsToken() {
        log.info("获取阿里云配置");
        final StsTokenVo stsTokenVo = (StsTokenVo) redisTemplate.opsForValue().get("fsp-sts");
        if (stsTokenVo != null) {
            return BaseResponse.success(stsTokenVo);
        }
        final StsTokenVo sts = simpleOssClient.getSts();
        redisTemplate.opsForValue().set("auto-insurance-sts", sts, 3500, TimeUnit.SECONDS);
        log.info("获取阿里云配置响应结果 aliyun = {}",sts);
        return new BaseResult(APICode.SUCCESS.getCode(),APICode.SUCCESS.getMessage(),sts);
    }
}
